<?php
class View
{
	protected static $vars	=	array();	//全局共享的模板变量
	protected static $layout;		//页面布局
	
	
	static function assign($data)
	{
		self::$vars	=	array_merge(self::$vars, $data);
	}
	static function pushVar($k, $v)
	{
		self::$vars[$k]	=	$v;
	}
	
	static function fetch($_act, $_layout=null)
	{
		ob_flush();
		self::display($_act, $_layout);
		return ob_get_clean();
	}
	
	static function display($_act, $_layout=null)
	{
		//error_reporting(E_ALL & ~E_NOTICE);
		
		if(!$_act)
		{
			$_act	=	$_REQUEST['a'];
		}
		else
		{
			$_act	=	strtolower($_act);
			false===strpos($_act,'.')?$_act.='.show':null;
		}
		
		extract(self::$vars, EXTR_OVERWRITE);	//变量的作用域只当前文件

		//$_include_path	=	get_include_path();
		set_include_path(get_include_path() .PATH_SEPARATOR. Lib::path('%/'));
		
		//自定义变量
		$root	=	WEB_ROOT;
		$theme	=	WEB_THEME;
		$skin	=	WEB_THEME.'/skin';

		if(false===$_layout)
		{
			try {
				include self::tpl($_act);
			}
			catch(Exception $_){
				Show::error("Error when load template: '{$_act}'");
			}
		}
		else
		{
			$_layout ? null : $_layout = (self::$layout ? self::$layout :Core::cfg('layout'));
			if($_layout)
			{
				$_layfile	=	Lib::path('%/FASHION/'.$_layout.'.php');
				if(!$_layfile)
				{
					die("Error when load layout: '{$_layout}'");
				}
				try {
					$tpl_file	=	self::tpl($_act);
				}
				catch(Exception $_){
					die("Error when load template: '{$_act}'");
				}
				
				if(!Core::cfg('debug') && Core::cfg('extend'))
				{
					$_cfile		=	DATA_PATH.'/cache/%%'.$_layout.str_replace('/','%',substr($tpl_file,strlen(APP_PATH)));
					if(
						!file_exists($_cfile)
						//|| (0===time()%3)
						//&& (max(filemtime($tpl_file),filemtime($_layfile))>filemtime($_cfile))
					)
					{
						$GLOBALS['_TPL_STR']	=	self::extend($tpl_file);
						file_put_contents($_cfile, self::extend($_layfile));
						unset($GLOBALS['_TPL_STR']);
					}
					include $_cfile;
				}
				else
				{
					include $_layfile;
				}
			}
			else
			{
				try {
					include self::tpl($_act);
				}
				catch(Exception $_){
					die("Error when load template: '{$_act}'");
				}
			}
		}
		
		//set_include_path($_include_path);
	}
	
	/**
	 * remove the extend files all
	 *
	 */
	static function clearCache($theme=null)
	{
		$dp	=	opendir(DATA_PATH.'/cache/');
		if(!$dp) return ;
		
		if($theme!==null)
		{
			$len	=	strlen("%%$theme");
			while($f=readdir($dp))
			{
				if('%%'.$theme===substr($f,0,$len))
				{
					unlink(DATA_PATH.'/cache/'.$f);
				}
			}
		}
		else
		{
			while($f=readdir($dp))
			{
				if('%%'===substr($f,0,2))
				{
					unlink(DATA_PATH.'/cache/'.$f);
				}
			}
		}
		closedir($dp);
	}
	
	static protected function extend($tfile)
	{
		return preg_replace_callback('|<\?php\s+include(.*?)\?\>|i', array(__CLASS__,'_EXT_CB'), file_get_contents($tfile));
	}
	static protected function _EXT_CB($str)
	{
		$tmp	=	str_replace(array('\'','"',';',' '), '', $str[1]);
		if('$tpl_file'===$tmp)
		{
			return isset($GLOBALS['_TPL_STR']) ? $GLOBALS['_TPL_STR'] : $str[0];
		}
		elseif('$'===substr($tmp,0,1))
		{
			return $str[0];
		}
		else
		{
			return file_get_contents(Lib::path('%/'.$tmp));
		}
	}
	
	/**
	 * 设置页面布局
	 */
	static function setLayout($layout)
	{
		self::$layout	=	$layout;
	}
	/**
	 * 设置Content-type，如 "text/html","text/vnd.wml"
	 */
	static function setContentType($type)
	{
		header('Content-type: '.$type.'; charset=utf-8');
	}
	/** 设置或取到当前系统使用的主题  */
	static function theme($theme=null)
	{
		static $t;
		if($theme)
		{
			return $t = $theme;
		}
		return $t?$t:Core::cfg('theme');
	}
	
	/**
	 * 通过action取到模板文件名
	 */
	static function tpl($act=null)
	{
		$tmp	=	explode('.', strtolower($act));
		return Lib::path('%/'.$tmp[0].'/'.$tmp[1].'.php');
	}
	
}
?>